package com.toefl.speaking.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.toefl.speaking.R;
import com.toefl.speaking.activity.base.StepListActivity;
import com.toefl.speaking.dao.bean.QuestionBean;
import com.toefl.speaking.handler.QuestionHandler;

public class QuestionListActivity extends StepListActivity<QuestionBean> implements OnClickListener {

	private final static String TAG = "QuestionListActivity";
	private static int questionType;
	private int favoritesItemID;
	private static ArrayList<QuestionBean> questiontList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.context = QuestionListActivity.this;
		super.onCreate(savedInstanceState);
		Intent intent = this.getIntent();
		questionType = intent.getExtras().getInt("question_type");
		super.setContentView(R.layout.question_list_layout);
		load();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.question_list_item, null);
			// 绑定事件
			convertView.findViewById(R.id.image).setOnClickListener(this);
			convertView.findViewById(R.id.text).setOnClickListener(this);
		}

		final QuestionBean questionBean = getItem(position);
		convertView.setTag(position);
		if (questionBean != null) {
			((TextView) convertView.findViewById(R.id.text))
					.setText((position + 1) + "." + questionBean.question_title);
			((TextView) convertView.findViewById(R.id.text)).setTag(position);
			convertView.findViewById(R.id.image).setTag(position);
		}
		return convertView;
	}

	@Override
	protected ArrayList<QuestionBean> loadData() {
		ArrayList<QuestionBean> questionList = postQuestionList();
		return questionList;
	}

	@Override
	protected void userOnPost() {
		// TODO Auto-generated method stub
		super.userOnPost();
	}

	@Override
	public void userDoInBackground() {
		// postDeleteFavorites(map2JsonString(new int[] { 1 }));
		postFavorites(map2JsonString(new int[] { favoritesItemID }));
		super.userDoInBackground();
	}

	@Override
	public void onClick(View v) {
		int position = Integer.parseInt(v.getTag().toString());
		// DialogHelper.showToastCanceledOnTouchOutside(this, null, v.getTag().toString());
		QuestionBean question = questiontList.get(position);
		Intent intent = new Intent();
		intent.putExtra("question_id", question.question_id);
		intent.putExtra("question_type", questionType);
		switch (v.getId()) {
		case R.id.text:
			MainActivity.switchActivity(QuestionActivity.class, intent);
			break;
		case R.id.image:
			favoritesItemID = question.question_id;
			getUserAsyncTask().execute();
			break;
		}

	}

	/**
	 * 获取标题列表
	 * @return
	 */
	public ArrayList<QuestionBean> postQuestionList() {
		QuestionHandler questionHandler = QuestionHandler.instance();
		questiontList = new ArrayList<QuestionBean>();
		String result = null;
		try {
			result = questionHandler.postQuestionList(String.valueOf(questionType));
			JSONObject jsonResult = new JSONObject(result);
			JSONArray questionListJson = jsonResult.getJSONArray("QuestionList");
			for (int i = 0; i < questionListJson.length(); i++) {
				JSONObject questionJson = (JSONObject) questionListJson.get(i);
				QuestionBean questionBean = new QuestionBean();
				questionBean.question_id = questionJson.getInt("Qid");
				questionBean.question_title = questionJson.getString("Title");
				// questionBean
				questiontList.add(questionBean);
			}
			Log.i(TAG, result);
		} catch (Exception e) {

		}
		return questiontList;
	}

	/**
	 * 添加收藏
	 * @param favoritesJson
	 */
	public static void postFavorites(String favoritesJson) {
		QuestionHandler questionHandler = QuestionHandler.instance();
		String result = null;
		try {
			result = questionHandler.postAddFavorites(String.valueOf(questionType), favoritesJson);
			JSONObject jsonResult = new JSONObject(result);
			JSONArray questionListJson = jsonResult.getJSONArray("QuestionList");
			for (int i = 0; i < questionListJson.length(); i++) {
				JSONObject questionJson = (JSONObject) questionListJson.get(i);
				QuestionBean questionBean = new QuestionBean();
				questionBean.question_id = questionJson.getInt("Qid");
				questionBean.question_title = questionJson.getString("Title");
			}
			Log.i(TAG, result);
		} catch (Exception e) {
		}
	}

	/**
	 * 删除收藏
	 * @param favoritesJson
	 */
	public static void postDeleteFavorites(String favoritesJson) {
		QuestionHandler questionHandler = QuestionHandler.instance();
		String result = null;
		try {
			result = questionHandler.postDeleteFavorites(String.valueOf(questionType), favoritesJson);
			JSONObject jsonResult = new JSONObject(result);
			JSONArray questionListJson = jsonResult.getJSONArray("QuestionList");
			for (int i = 0; i < questionListJson.length(); i++) {
				JSONObject questionJson = (JSONObject) questionListJson.get(i);
				QuestionBean questionBean = new QuestionBean();
				questionBean.question_id = questionJson.getInt("Qid");
				questionBean.question_title = questionJson.getString("Title");
			}
			Log.i(TAG, result);
		} catch (Exception e) {
		}
	}

	public static String map2JsonString(int[] qidMap) {
		JSONArray jsonArray = new JSONArray();
		for (int i = 0; i < qidMap.length; i++) {
			jsonArray.put(qidMap[i]);
		}
		Log.i(TAG, jsonArray.toString());
		return jsonArray.toString();
	}
}
